/* Copyright (c) 2008 LepomisLab.com, Andrey Turkin
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "NSTextFieldCell.h"
#include "NSKeyedUnarchiver.h"
#include "NSView.h"

IMPLEMENT_DYNAMIC_CLASS(NSTextFieldCell, NSActionCell)

NSTextFieldCell::NSTextFieldCell() : NSActionCell(), _password(false)
{
	setValue(wxT("drawsBackground"), true);
	this->registerObserver(this, wxT("editable")); // who said we cannot observe ourselves? :)
	this->registerObserver(this, wxT("objectValue"));
	exposeBinding(wxT("value"));
	disableAutomaticObservation(wxT("value"));
}

void NSTextFieldCell::_setValue(const wxString& key, wxObject *value)
{
	if (key == wxT("drawsBackground"))
	{
		wxVariant *v = wxDynamicCast(value, wxVariant);
		wxASSERT(v);
		_drawsBackground = v->GetBool();
	} else if (key == wxT("value"))
	{
		setValue(wxT("objectValue"), value);
	} else
		NSActionCell::_setValue(key, value);
}

wxObject* NSTextFieldCell::_getValue(const wxString& key)
{
	if (key == wxT("drawsBackground"))
	{
		return new wxVariant(_drawsBackground);
	} else if (key == wxT("value"))
	{
		return getValue(wxT("objectValue"));
	} else
		return NSActionCell::_getValue(key);
}

void NSTextFieldCell::_willValueChangedForKey(const wxString &key)
{
	NSActionCell::_willValueChangedForKey(key);
	if (key == wxT("objectValue"))
		_willValueChangedForKey(wxT("value"));
}

void NSTextFieldCell::_didValueChangedForKey(const wxString &key)
{
	if (key == wxT("objectValue"))
		_didValueChangedForKey(wxT("value"));
	NSActionCell::_didValueChangedForKey(key);
}


void NSTextFieldCell::initWithUnarchiver(NSKeyedUnarchiver *archiver)
{
	NSActionCell::initWithUnarchiver(archiver);
	archiver->decodeObjectForKey(wxT("NSTextColor"));
	archiver->decodeObjectForKey(wxT("NSBackgroundColor"));
	
	if (archiver->contains(wxT("NSDrawsBackground")))
		setValue(wxT("drawsBackground"), archiver->decodeBoolForKey(wxT("NSDrawsBackground")));
}

void NSTextFieldCell::_releaseWidget(wxWindow *parent)
{
	NSUNUSED(parent);
	delete _widget;
}

void NSTextFieldCell::observeValueForKeypath(const wxString &keypath, NSObject* object, void *context)
{
	if (this==object && _widget)
	{
		wxTextCtrl *w = wxDynamicCast(_widget, wxTextCtrl);
		if (w)
		{
			if (keypath == wxT("editable"))
				w->SetEditable(_editable);
			else if (keypath == wxT("objectValue"))
				w->ChangeValue(getContents());
		} else {
			wxStaticText* ww = wxDynamicCast(_widget, wxStaticText);
			wxASSERT(ww);
			if (keypath == wxT("objectValue"))
				ww->SetLabel(getContents());
		}
	}
	NSActionCell::observeValueForKeypath(keypath, object, context);
}

class MyTextCtrlEvt : public wxEvtHandler
{
public:
	MyTextCtrlEvt(NSTextFieldCell* cell, wxTextCtrl* ctrl) : wxEvtHandler(), _cell(cell), _ctrl(ctrl){}
	void OnChange(wxCommandEvent& evt)
	{
		NSUNUSED(evt);
		_cell->setValue(wxT("objectValue"), new wxVariant(_ctrl->GetValue()));
	}
private:
	NSTextFieldCell* _cell;
	wxTextCtrl* _ctrl;
};

void NSTextFieldCell::_allocWidget(wxWindow *parent)
{
	bool _draws_background = getBoolValue(wxT("drawsBackground"));
	if (!_editable /*&& !_draws_background*/)
	{
		wxASSERT(!_password);
		_widget = new wxStaticText(parent, wxID_ANY, getContents());
	} else {
		wxTextCtrl* ctrl = new wxTextCtrl(parent, wxID_ANY, getContents(), wxDefaultPosition, wxDefaultSize, _password ? wxTE_PASSWORD : 0);
		ctrl->SetEditable(_editable);
		ctrl->ChangeValue(getContents());
		ctrl->Connect(wxEVT_COMMAND_TEXT_UPDATED, wxCommandEventHandler(MyTextCtrlEvt::OnChange), NULL, new MyTextCtrlEvt(this, ctrl));
		_widget = ctrl;
	}
	controlView()->_initWidget(_widget);
}

